/*
 * TMap Designer - A Thematic Map design software
 * Copyright (C) 2006 World Health Organization
 * http://www.who.int
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
package who.tmap.ui.util;

import java.awt.Component;
import java.awt.Cursor;
import java.awt.Point;
import java.awt.Toolkit;

import org.apache.log4j.Logger;

import who.tmap.util.MiscHelper;

/**
 * A helper dedicated to cursors for the map window
 * @author Administrator
 *
 */
public class CursorHelper {
	
    private static Logger logger = Logger.getLogger(CursorHelper.class);  	

    public enum Cursors { DEFAULT, WAIT, MOVE, ADD_POINT, DEL_POINT, FILL, LINE, 
    					  TEXT, SPLIT, MERGE_CB1, MERGE_CB2, MERGE_CB3, 
    					  LABELS_LIST, ZOOM_IN, ZOOM_OUT, IMAGE };
	
	private static Cursor[] cursors =null;
	
    private static Cursor prevCursor =null;
    
    private static final String CURSOR_DIR ="cursors";
    
    // Only used during creation of the cursors
    private MiscHelper miscUIHelper =null;
    
    public CursorHelper() {
    	if ( cursors == null ) {
    		init();
    	}
    }
    
    private void init() {
//   	logger.info( "Maximum cursor colors supported=" + Toolkit.getDefaultToolkit().getMaximumCursorColors() );
//		logger.info( "Best cursor size=" + Toolkit.getDefaultToolkit().getBestCursorSize(32, 32) );
		
		cursors =new Cursor[ Cursors.values().length ];
		
		miscUIHelper =new MiscHelper();
		
		cursors[ Cursors.DEFAULT.ordinal() ] =new Cursor(Cursor.DEFAULT_CURSOR);
		cursors[ Cursors.WAIT.ordinal() ] =new Cursor(Cursor.WAIT_CURSOR);
		cursors[ Cursors.MOVE.ordinal() ] =createCustomCursor( "pointeur_move.gif" );
		cursors[ Cursors.ADD_POINT.ordinal() ] =createCustomCursor( "pointeur_createpoint.gif" );
		cursors[ Cursors.DEL_POINT.ordinal() ] =createCustomCursor( "pointeur_deletepoint.gif" );
		cursors[ Cursors.FILL.ordinal() ] =createCustomCursor( "pointeur_fill.gif", new Point( 0, 19 ) );    		
		cursors[ Cursors.LINE.ordinal() ] =createCustomCursor( "pointeur_line.gif", new Point( 0, 15 ) );    		
		cursors[ Cursors.TEXT.ordinal() ] =createCustomCursor( "pointeur_text.gif", new Point( 1, 17 ) );    		
		cursors[ Cursors.SPLIT.ordinal() ] =createCustomCursor( "pointeur_border.gif" );    		
		cursors[ Cursors.MERGE_CB1.ordinal() ] =createCustomCursor( "pointeurs_step1.gif" );    		
		cursors[ Cursors.MERGE_CB2.ordinal() ] =createCustomCursor( "pointeurs_step2.gif" );
		cursors[ Cursors.MERGE_CB3.ordinal() ] =createCustomCursor( "pointeurs_step3.gif" );    
		cursors[ Cursors.LABELS_LIST.ordinal() ] =createCustomCursor( "pointeur_crealist.gif" ); 	
		cursors[ Cursors.ZOOM_IN.ordinal() ] =createCustomCursor( "pointeur_zoom_in.gif" ); 
		cursors[ Cursors.ZOOM_OUT.ordinal() ] =createCustomCursor( "pointeur_zoom_out.gif" ); 	
		cursors[ Cursors.IMAGE.ordinal() ] =createCustomCursor( "pointeur_image.gif", new Point( 1, 17 ) ); 		
    }
    
    private Cursor createCustomCursor( String cursorFileName ) {
    	return createCustomCursor( cursorFileName, new Point(0,0) );
    }
    
    private Cursor createCustomCursor( String cursorFileName, Point pt ) {
    	return Toolkit.getDefaultToolkit().createCustomCursor( miscUIHelper.getImageIcon( CURSOR_DIR + "/" + cursorFileName ).getImage(),
                				   							   pt, cursorFileName );
    }    
    
    public void setCursor( Component c, Cursors cursorVal ) {
    	prevCursor =c.getCursor();
    	c.setCursor( cursors[ cursorVal.ordinal() ] );
    }
    
    public void setPreviousCursor( Component c ) {
        c.setCursor( prevCursor );
    }	
	
}
